/**
 * 
 */
package com.mbengue.corp.apr.ra.dao.impl;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.mbengue.corp.apr.ra.core.Article;
import com.mbengue.corp.apr.ra.core.Membre;
import com.mbengue.corp.apr.ra.dao.ArticleDao;
import com.mbengue.corp.apr.ra.util.HibernateUtil;

/**
 * @author MMB11447
 *
 */
@SuppressWarnings("unchecked")
public class ArticleDaoImpl implements ArticleDao, Serializable {
  private static final long serialVersionUID = 1L;
  private Session session;

  public ArticleDaoImpl() {
    session = HibernateUtil.getSessionFactory();
  }

  /* (non-Javadoc)
   * @see com.mbengue.corp.apr.ra.dao.ArticleDao#ajouterArticle(com.mbengue.corp.apr.ra.core.Article)
   */
  @Override
  public void ajouterArticle(Article article) throws HibernateException {
    Transaction transaction = session.beginTransaction();
    try {
      session.save(article);
      transaction.commit();
    } catch (Exception e) {
      transaction.rollback();
      throw new HibernateException(e);
    } finally {
      session.flush();
      //session.close();
    }
  }

  /* (non-Javadoc)
   * @see com.mbengue.corp.apr.ra.dao.ArticleDao#trouverArticle(java.lang.String)
   */
  @Override
  public Article trouverArticle(String titre) throws HibernateException {
    Criteria criteria = session.createCriteria(Article.class);
    criteria.add(Restrictions.idEq(titre));
    return (Article) criteria.uniqueResult();
  }

  /* (non-Javadoc)
   * @see com.mbengue.corp.apr.ra.dao.ArticleDao#modifierArticle(com.mbengue.corp.apr.ra.core.Article)
   */
  @Override
  public void modifierArticle(Article article) throws HibernateException {
    Transaction transaction = session.beginTransaction();
    try {
      session.update(article);
      transaction.commit();
    } catch (Exception e) {
      transaction.rollback();
      throw new HibernateException(e);
    } finally {
      session.flush();
      //session.close();
    }
  }

  /* (non-Javadoc)
   * @see com.mbengue.corp.apr.ra.dao.ArticleDao#tousLesArticles()
   */
  @Override
  public List<Article> tousLesArticles() throws HibernateException {
    //    if (!session.isOpen()) {
    //      session.beginTransaction();
    //    }
    Query query = session.getNamedQuery("articles");
    query.setString("type", "Article");
    return query.list();
  }

  /* (non-Javadoc)
   * @see com.mbengue.corp.apr.ra.dao.ArticleDao#lesArticlesD1Auteur(com.mbengue.corp.apr.ra.core.Membre)
   */
  @Override
  public List<Article> lesArticlesD1Auteur(Membre membre) throws HibernateException {
    Query query = session.getNamedQuery("articlesD1Auteur");
    query.setString(1, membre.getEmail());
    return query.list();
  }

  /* (non-Javadoc)
   * @see com.mbengue.corp.apr.ra.dao.ArticleDao#lesArticlesRecents()
   */
  @Override
  public List<Article> lesArticlesRecents() throws HibernateException {
    int maxResult = 4;
    Criteria criteria = session.createCriteria(Article.class);
    criteria.addOrder(Order.desc("dateDeCreation"));
    criteria.setMaxResults(maxResult);
    return criteria.list();
  }

  /* (non-Javadoc)
   * @see com.mbengue.corp.apr.ra.dao.ArticleDao#tousLesPvDeReunion()
   */
  @Override
  public List<Article> tousLesPvDeReunion() throws HibernateException {
    Query query = session.getNamedQuery("articles");
    query.setString("type", "PV Reunion");
    return query.list();
  }

  @Override
  public List<Article> findArticlesParDates(Date aDebut, Date aFin, String type) {
    Criteria criteria = session.createCriteria(Article.class);
    criteria.add(Restrictions.between("dateDeCreation", aDebut, aFin));
    criteria.add(Restrictions.eq("type", type));
    return criteria.list();
  }

  @Override
  public List<Article> findArticlesParTitre(String aTitre, String type) {
    Criteria criteria = session.createCriteria(Article.class);
    criteria.add(Restrictions.like("titre", "%" + aTitre + "%"));
    criteria.add(Restrictions.eq("type", type));
    return criteria.list();
  }

  @Override
  public boolean supprimerArticle(Article aArticle) {
    Transaction transaction = session.beginTransaction();
    try {
      session.delete(aArticle);
      transaction.commit();
      return true;
    } catch (Exception ex) {
      ex.printStackTrace();
      return false;
    }
  }
}
